//
// Created by 34746 on 2022/4/16.
//
#include "src/TGLine.h"
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/opencv.hpp"
#include <bits/stdc++.h>
using namespace std;
using namespace cv;

RNG rng(time(0));

Scalar bgColor(255, 255, 255);
Scalar lineColor(150, 0, 0);

void drawLine(Mat& img, TGLine& l) {
    line(img, Point2d(l.data.first.x*100, l.data.first.y*100),
         Point2d(l.data.second.x*100,l.data.second.y*100),
         Scalar(255, 0, 0), 3);
}

void drawPoint(Mat& img, TGPoint& p) {
    circle(img, Point2d(p.x*100, p.y*100), 10, Scalar(0, 255, 0), 2);
}

int rand10(RNG& rng) {
    return rng.uniform(0, 11);
}


void run() {
    int x1,y1,x2,y2,x3,y3,x4,y4;
    TGLine l1, l2;
    x1 = rand10(rng);
    y1 = rand10(rng);
    x2 = rand10(rng);
    y2 = rand10(rng);
    x3 = rand10(rng);
    y3 = rand10(rng);
    x4 = rand10(rng);
    y4 = rand10(rng);
    l1.data.first.x = x1;
    l1.data.first.y = y1;
    l1.data.second.x = x2;
    l1.data.second.y = y2;
    l2.data.first.x = x3;
    l2.data.first.y = y3;
    l2.data.second.x = x4;
    l2.data.second.y = y4;
    TGPoint p0;
    bool flag = intersection(l1, l2, p0);
    Mat backGround = Mat(1000, 1000, CV_8UC3, bgColor);
    drawLine(backGround, l1);
    drawLine(backGround, l2);
    if (flag) drawPoint(backGround, p0);
    imshow("Main", backGround);
}


void onMouse(int event, int x, int y, int flags, void* param) {
    if(event == EVENT_LBUTTONDOWN) {
        run();
    }
}

int main() {
    run();
    setMouseCallback("Main", onMouse);
    waitKey(0);
    destroyAllWindows();
    return 0;
}